import sqlite3
import os

def verify_data_cleared():
    """验证数据库中的测试数据是否已完全清空"""
    # 获取数据库文件路径
    db_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "database.db")
    print(f"验证数据库文件: {db_path}")
    
    try:
        # 连接数据库
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        
        # 检查关键表的数据量
        tables_to_check = [
            ("orders", "订单表"),
            ("products", "商品表"),
            ("categories", "分类表"),
            ("'3d_models'", "3D模型表"),  # 使用引号包装特殊表名
            ("product_images", "商品图片表"),
            ("model3d_uploads", "3D模型上传表")
        ]
        
        all_cleared = True
        print("\n=== 数据清理验证结果 ===")
        
        for table_sql, table_name in tables_to_check:
            try:
                cursor.execute(f"SELECT COUNT(*) FROM {table_sql}")
                count = cursor.fetchone()[0]
                print(f"{table_name}: {count} 条记录")
                
                if count > 0:
                    all_cleared = False
                    print(f"⚠️ {table_name} 中仍有数据！")
                    
                    # 如果有数据，显示前5条记录作为示例
                    try:
                        print(f"  前2条记录示例:")
                        cursor.execute(f"SELECT * FROM {table_sql} LIMIT 2")
                        rows = cursor.fetchall()
                        for row in rows:
                            print(f"  {row}")
                    except:
                        print("  无法显示记录详情")
            except Exception as e:
                print(f"❌ 检查 {table_name} 时出错: {e}")
                all_cleared = False
        
        print("\n=== 验证结论 ===")
        if all_cleared:
            print("✅ 所有测试数据已成功清空！")
        else:
            print("❌ 部分表中仍有数据，请检查")
            
        return all_cleared
        
    except Exception as e:
        print(f"数据库操作错误: {e}")
        return False
    finally:
        if 'conn' in locals():
            conn.close()
            print("\n数据库连接已关闭")


if __name__ == "__main__":
    print("开始验证数据清理结果...")
    verify_data_cleared()
    print("\n验证完成。请刷新后台页面查看最新状态。")